//
//  CZStatusOriginalView.swift
//  WeiBo
//
//  Created by apple on 16/6/27.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit
class CZStatusOriginalView: UIView {
    // 声明一个全局的原创微博底部约束的供我们卸载
    var originalViewBottomConstraint: Constraint?
    // 供外界设置数据
    var status: CZStatus? {
        didSet{
            // 用户昵称
            nameLabel.text = status?.user?.name
            // 用户头像
            headImageView.sd_setImageWithURL(NSURL(string: status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
            // 时间
            timeLabel.text = getWbTimeStr(status?.created_at ?? "")
            // 来源
            sourceLabel.attributedText = status?.sourceAttr
            // 微博内容
            contentLabel.text = status?.text
            // 认证图片
            vipImageView.image = status?.user?.verifiedImage
            // 会员等级
            expiredImageView.image = status?.user?.mbrankImage
            
            //先卸载
            originalViewBottomConstraint?.uninstall()
            // 代表有配图
            if status?.pic_urls?.count > 0 {
                // 显示配图
                pictureView.hidden = false
                // 赋值
                pictureView.pic_urls = status?.pic_urls
                // 更新约束
                self.snp_makeConstraints { (make) -> Void in
                    originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(StautsMargin).constraint
                }
            }else {
                // 代表没有配图
                // 隐藏配图
                pictureView.hidden = true
                // 更新约束
                self.snp_makeConstraints { (make) -> Void in
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(StautsMargin).constraint
                }
            }
        }
    }
    
   
    // 处理微博时间方法
    func getWbTimeStr(creatAt: String) -> String{
  
        // 时间格式
        let df = NSDateFormatter()
        // 格式
        df.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
        // 设置地区
        df.locale = NSLocale(localeIdentifier: "en_US")
        // 微博时间
        let wbDate = df.dateFromString(creatAt)!
     
        
        // 判断是否是今年
        let isThisYear: Bool = getWbIsThisYear(wbDate)
        // 代表是今年
        if isThisYear {
             // 日历类
             let calendar =  NSCalendar.currentCalendar()
            // 如果是今天
            if calendar.isDateInToday(wbDate) {
                // 秒数差值
                let s = NSDate().timeIntervalSinceDate(wbDate)
                //如果小于60秒 显示格式: 刚刚
                if s < 60 {
                    return "刚刚"
                }else if s >= 60 && s < 60 * 60 {
                    //显示格式:xx分钟前
                    return "\(Int(s/60))分钟前"
                }else{
                    //显示格式: xx小时前
                    return "\(Int(s/3600))小时前"
                }
                
            }else if calendar.isDateInYesterday(wbDate){
                // 昨天 显示格式: 昨天 12:12
                df.dateFormat = "昨天 HH:mm"
                return df.stringFromDate(wbDate)
            }else {
                // 其他 显示格式: 06月12日 13:13
                df.dateFormat = "MM月dd日 HH:mm"
                return df.stringFromDate(wbDate)
            }
            
            
        }else {
           // 代表不是今年 显示格式: 2015年05月05日 10:10
            df.dateFormat = "yyyy年MM月dd日 HH:mm"
            let result = df.stringFromDate(wbDate)
            return result
        }
    }
    // 判断微博时间是否是今年
    func getWbIsThisYear(wbDate: NSDate) -> Bool{
        let df = NSDateFormatter()
        // 设置地区
        df.locale = NSLocale(localeIdentifier: "en_US")
        // 格式
        df.dateFormat = "yyyy"
        // 微博时间
        let wbTimeStr = df.stringFromDate(wbDate)
        // 当前时间
        let currentTimeStr = df.stringFromDate(NSDate())
        // 代表是今年
        if wbTimeStr == currentTimeStr {
            return true
        }else {
            // 代表不是今年
            return false
        }
    }
    

    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置视图
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        
        // 添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(expiredImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(vipImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        // 添加约束
        // 头像
        headImageView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(StautsMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        // 昵称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(StautsMargin)
        }
        // 用户等级
        expiredImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(StautsMargin)
            make.centerY.equalTo(nameLabel)
        }
        // 发布时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        // 来自
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(StautsMargin)
        }
        // 认证
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        // 微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(StautsMargin)
        }
        // 原创微博配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(StautsMargin)
        }
        
        // 原创微博
        self.snp_makeConstraints { (make) -> Void in
            originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(StautsMargin).constraint
        }
        
        

        
    }
    
    //  懒加载控件
    // 头像
    private lazy var headImageView: UIImageView = UIImageView(imgName: "avatar_default_big")
    // 用户昵称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15)
    // 用户等级
    private lazy var expiredImageView: UIImageView = UIImageView(imgName: "common_icon_membership_expired")
    // 发布微博事件
    private lazy var timeLabel: UILabel = UILabel(textColor: ThemeColor, textFont: 10)
    // 来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.grayColor(), textFont: 10)
    // VIP
    private lazy var vipImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
// 微博内容
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.blackColor(), textFont: 15, maxWidth: SCREENW - 2*StautsMargin)
    // 原创微博配图
    private lazy var pictureView: CZPictureView = CZPictureView()
}

extension CZStatusOriginalView {
    
    func getMbrankImage(mbrank: Int) -> UIImage? {
        print("会员等级")
        
        if mbrank > 0 && mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }else {
        
            return UIImage(named: "common_icon_membership_expired")
        }
        
    }
    
    
    func getVipImage(verified: Int) -> UIImage? {
        print("认证类型")
        switch verified{
        case 0:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return UIImage(named: "avatar_vgirl")
        }
    }
}



